home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / GETOPT3.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  3KB  |  103 lines

  1. /*
  2.  * *    @(#)getopt.c    2.3 (smail) 5/30/87 
  3.  */
  4.  
  5. /*
  6.  * Here's something you've all been waiting for:  the AT&T public domain
  7.  * source for getopt(3).  It is the code which was given out at the 1985
  8.  * UNIFORUM conference in Dallas. I obtained it by electronic mail directly
  9.  * from AT&T.  The people there assure me that it is indeed in the public
  10.  * domain. 
  11.  *
  12.  * There is no manual page. That is because the one they gave out at UNIFORUM
  13.  * was slightly different from the current System V Release 2 manual page. 
  14.  * The difference apparently involved a note about the famous rules 5 and 6,
  15.  * recommending using white space between an option and its first argument,
  16.  * and not grouping options that have arguments. Getopt itself is currently
  17.  * lenient about both of these things White space is allowed, but not
  18.  * mandatory, and the last option in a group can have an argument.  That
  19.  * particular version of the man page evidently has no official existence,
  20.  * and my source at AT&T did not send a copy. The current SVR2 man page
  21.  * reflects the actual behavor of this getopt. However, I am not about to
  22.  * post a copy of anything licensed by AT&T. 
  23.  */
  24.  
  25. #ifdef BSD
  26.  #include <strings.h>
  27. #else
  28.  #define index strchr
  29.  #include <string.h>
  30. #endif
  31.  
  32. /* LINTLIBRARY */
  33.  
  34. #define NULL    0
  35. #define EOF    (-1)
  36. #define ERR(s, c)    if(opterr){\
  37.     extern int write(int, void *, unsigned);\
  38.     char errbuf[2];\
  39.     errbuf[0] = (char)c; errbuf[1] = '\n';\
  40.     (void) write(2, strlwr(argv[0]), (unsigned)strlen(argv[0]));\
  41.     (void) write(2, s, (unsigned)strlen(s));\
  42.     (void) write(2, errbuf, 2);}
  43.  
  44. extern char    *index();
  45.  
  46. int             opterr = 1;
  47. int             optind = 1;
  48. int             optopt;
  49. char           *optarg;
  50.  
  51. int getopt(int argc, char *argv[], char *opts)
  52. {
  53.       static int      sp = 1;
  54.       register int    c;
  55.       register char  *cp;
  56.  
  57.       if (sp == 1)
  58.       {
  59.             if (optind >= argc || argv[optind][0] != '-' ||
  60.                   argv[optind][1] == '\0')
  61.                         return (EOF);
  62.             else if (strcmp(argv[optind], "--") == NULL)
  63.             {
  64.                   optind++;
  65.                   return (EOF);
  66.             }
  67.       }
  68.       optopt = c = argv[optind][sp];
  69.       if (c == ':' || (cp = index(opts, c)) == NULL)
  70.       {
  71.             ERR(": illegal option -- ", c);
  72.             if (argv[optind][++sp] == '\0')
  73.             {
  74.                   optind++;
  75.                   sp = 1;
  76.             }
  77.             return ('?');
  78.       }
  79.       if (*++cp == ':')
  80.       {
  81.             if (argv[optind][sp + 1] != '\0')
  82.                   optarg = &argv[optind++][sp + 1];
  83.             else if (++optind >= argc)
  84.             {
  85.                   ERR(": option requires an argument -- ", c);
  86.                   sp = 1;
  87.                   return ('?');
  88.             }
  89.             else  optarg = argv[optind++];
  90.             sp = 1;
  91.       }
  92.       else
  93.       {
  94.             if (argv[optind][++sp] == '\0')
  95.             {
  96.                   sp = 1;
  97.                   optind++;
  98.             }
  99.             optarg = NULL;
  100.       }
  101.       return (c);
  102. }
  103.